package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityGroupManagerBinding
import com.haoshuang.zhouzhoubang.model.bean.TeamGroupBean
import com.haoshuang.zhouzhoubang.ui.adapter.GroupManagerAdapter
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.haoshuang.zhouzhoubang.ui.dialog.EditTeamGroupDialog
import com.haoshuang.zhouzhoubang.ui.dialog.TeamGroupMenuDialog
import com.haoshuang.zhouzhoubang.ui.vm.GroupManagerViewModel
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.startPageForResult
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical


/**
 *
 */
class GroupManagerActivity : BaseVMActivity<GroupManagerViewModel, ActivityGroupManagerBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<GroupManagerActivity>()
        }
    }

    private val data = arrayListOf<TeamGroupBean>()
    private val adapter = GroupManagerAdapter(data)


    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "分组管理"
        binding.titleBar.leftImageView().click { finish() }

        binding.llAddGroup.clickWithTrigger {
            XPopup.Builder(this)
                .asCustom(EditTeamGroupDialog(this){
                    vm.createGroup("group_name" to it)
                })
                .show()
        }

        binding.recyclerView.vertical()
            .adapter = adapter

        adapter.setOnItemClick { view, position , item->
            XPopup.Builder(this)
                .asCustom(TeamGroupMenuDialog(this, item?.group_name?:""){
                    when(it){
                        1 -> {
                            // 编辑分组名称
                            XPopup.Builder(this)
                                .asCustom(EditTeamGroupDialog(this, item?.group_name?:""){
                                    vm.EditGroupName("group_id" to item?.id, "group_name" to it)
                                })
                                .show()
                        }
                        2 -> {
                            // 添加分组成员
                            startPageForResult<AddMemberActivity>("team_id" to team_id, "ids" to item?.member_list?.joinToString(","){it?.uid.toString()}){code, data ->
                                if (code == 200 && data != null){
                                    val ids = data.getString("ids")
                                    vm.addGroupMember("group_id" to item?.id, "member_ids" to ids)
                                }
                            }
                        }
                        3 -> {
                            // 移出分组成员
                            startPageForResult<RemoveMemberActivity>("group_id" to item?.id){code, data ->
                                if (code == 200 && data != null){
                                    val ids = data.getString("ids")
                                    vm.removeGroupMember("group_id" to item?.id, "member_ids" to ids)
                                }
                            }
                        }
                        4 -> {
                            // 删除分组
                            XPopup.Builder(this)
                                .asCustom(ConfirmDialog(this, content = "删除该分组后，当前分组内队员\n将会移到队员列表中查看",
                                    cancelStr = "再考虑考虑", confirmStr = "删除"){
                                    vm.delGroup("group_id" to item?.id)
                                })
                                .show()
                        }
                    }
                }).show()
        }

    }

    override fun onResume() {
        super.onResume()
        vm.getTeamGroup()
    }

    override fun initData() {

    }

    override fun startObserve() {

        vm.removeGroupMemberData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                vm.getTeamGroup()
            }
        }

        vm.addGroupMemberData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                vm.getTeamGroup()
            }
        }

        vm.delGroupData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                vm.getTeamGroup()
            }
        }

        vm.EditGroupNameData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                vm.getTeamGroup()
            }
        }

        vm.createGroupData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                vm.getTeamGroup()
            }
        }

        vm.getTeamGroupData.observeState(this){
            onLoading { showLoading()}
            onError {code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                data.clear()
                data.addAll(it?: emptyList())
                adapter.notifyDataSetChanged()
            }
        }

    }

}